<template>
  <div v-loading="httpLoading" element-loading-text="拼命加载中">
    <div class="head">
      <div class="logo"></div>
    </div>
    <div class="login-bd">
      <ul class="bd-inner">
        <li class="fl lg-poster"></li>
        <li class="fr lg-zone">
          <div class="lg-label">
            <h4>用户登录</h4>
          </div>
          <div class="line-y"></div>
          <div class="line-g"></div>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="账户" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <!-- <el-form-item label="验证码" prop="picLyanzhengma">
            <el-input style="width:40%" v-model="ruleForm.password"></el-input>
            <el-input style="width:40%" v-model="ruleForm.password"></el-input>
            </el-form-item> -->
            <el-form-item label="语言">
              <el-select v-model="ruleForm.region" placeholder="请选择语言">
                <el-option label="中文" value="0"></el-option>
                <el-option label="english" value="1"></el-option>
              </el-select>
            </el-form-item>
            <div class="clearfix remember">
              <el-checkbox class="fl remember-left" v-model="checked">记住用户名</el-checkbox>
              <router-link to="/register">
                <span class="fr remember-right">立即注册</span>
              </router-link>
              <router-link to="/ToCheckEmail">
                <span class="fr remember-right-r">忘记密码？</span>
              </router-link>
            </div>
            <div class="remember1">
              <el-button type="info" @click="submitForm('ruleForm','1')">采购登陆</el-button>
              <el-button type="primary" @click="submitForm('ruleForm','2')">供应商登录</el-button>
            </div>
          </el-form>
        </li>
      </ul>
    </div>
    <div class="footer-end">
      <p>苏ICP备 09003078号 E-mail：srm@elementspeed.com</p>
    </div>
  </div>
</template>

<script>
import "./../assets/css/common.css";
import "./../assets/css/base.css";
import qs from 'qs';
export default {
    data() {
      return {
        checked:true,
        value: '',
        ruleForm: {
          name: 'admin@qq.com',
          password: '123456',
          region: '0',
        },
        rules: {
          name: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 1, max: 30, message: '账号在 1 到 30 个字符之间', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 1, max: 30, message: '密码长度在 1 到 30 个字符之间', trigger: 'blur' }
          ]
        }
      };
    },
  computed:{
    httpLoading(){
      console.log(this.$store.state.httping)
      return this.$store.state.httping
    }
  },    
    methods: {
      submitForm(formName,userType) {
        this.$refs[formName].validate((valid) => {
          if (true) {
            this.ruleForm.userType = userType;
            this.axios.post(this.lib.config.host.system+'/system/login?a=1',qs.stringify(this.ruleForm))
            .then(res=>{
              sessionStorage.setItem("userName", res.data.resultMap.userName);  
              sessionStorage.setItem("vendorId", res.data.resultMap.vendorId);                   
              var result = res.data;
              if (result.resultMap.securityId) {
                this.$store.commit('updateSecurityId',result.resultMap.securityId);
              }
              if(result.success){
                if(result.resultMap.page){
                  this.$router.push('/Questionnaire'); 
                }else{
                  sessionStorage.setItem('optJsons', result.resultMap.optJsons);
                  this.$router.push('/Index')
                  if (this.$route.query.redirect) {
                   this.$router.push({path: decodeURIComponent(this.$route.query.redirect)});
                  } else
                  this.$router.push('/Index')
                  }
              }else{
                alert(result.message)
              }
            })
            .catch(error=>{
              console.log(error);
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }

</script>

<style scoped>
.el-input{
  width: 82%;
}

</style>
